<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('新闻列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>新闻标题：</label>
                                <input type="text" name="title"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="onAdd()"  >
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.editTab()"  >
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"  >
                    <i class="fa fa-remove"></i> 删除
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "news/content";

        $(function() {
            var options = {
                url: prefix + "/list?kind=NEWS",
                createUrl: prefix + "/newsAdd",
                updateUrl: prefix + "/newsEdit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "新闻",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'title',
                    title: '标题'
                },
                {
                    field: 'thumb',
                    title: '缩略图',
                    formatter: function(val) {
                        if(!val) return ''
                        return '<img class="cell-img" src="' + base + val + '" />'
                    }
                },
                {
                    field: 'brief',
                    title: '简报',
                    width: 200,
                    titleTooltip: true
                },
                {
                    field: 'idx',
                    title: '排序'
                },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function(val) {
                        if(val == 0) return '<span class="label label-warning">未发布</span>'
                        return '<span class="label label-info">已发布</span>'
                    }
                },
                {
                    field: 'hotFlag',
                    title: '热门标识',
                    formatter: function(val) {
                        if(val === '1') {
                            return '<span class="label label-success">是</span>'
                        } else {
                            return '<span class="label label-warning-light">否</span>'
                        }
                    }
                },
                {
                    field: 'viewNum',
                    title: '阅读数'
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    width: 150
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs " href="javascript:void(0)" onclick="$.operate.editTab(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a> ');
                        if(row.status != '1') {
                            actions.push('<a class="btn btn-info btn-xs" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="发布后可被查看和搜索" href="javascript:void(0)" onclick="onPublish(\'' + row.id + '\')"><i class="fa fa-level-up"></i>发布</a> ');
                        } else {
                            actions.push('<a class="btn btn-warning btn-xs" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="下架后不可被查看和搜索"  href="javascript:void(0)" onclick="onUnPublish(\'' + row.id + '\')"><i class="fa  fa-level-down"></i>下架</a> ');
                        }
                        actions.push('<a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="onView(\'' + row.id + '\')"><i class="fa  fa-eye"></i>预览</a> ');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        function onAdd() {
            $.operate.addTab()
        }

        function onPublish(id) {
            doUpdate(id, '1')
        }

        function onUnPublish(id) {
            doUpdate(id, '0')
        }

        function doUpdate(id, status) {
            $.operate.post(prefix + '/edit', {id: id, status: status}, function() {
                $.table.refresh();
            })
        }

        function onView(id) {
            $.modal.openTab("预览", prefix + '/newsPreview/' + id );
        }
    </script>
</body>
</html>